package com.zf.mall.user.service.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zf.mall.common.response.ErrorEnum;
import com.zf.mall.common.response.ResponseResultModel;
import com.zf.mall.user.ds.entity.User;
import com.zf.mall.user.service.ser.UserService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RefreshScope
@RestController
@RequestMapping("user")
public class UserServiceController {
	
	@Autowired
	UserService userService;
	
	@Value("${server.port}")
	private String port;

	@Value("${test.info}")
	private String info;

	@Value("${test.sleep}")
	private Long sleep;
	
	@PostMapping(value = "/register")
	public ResponseEntity<?> register(@RequestBody User user) {
		try {
			log.info("{}->开始注册用户 {} ",port , user);
			return userService.register(user);
		} catch (Exception e) {
			log.info("服务提供方=》注册服务异常 : {} " , e );
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	@PostMapping(value = "/login")
	public ResponseEntity<?> login(@RequestBody User user) {
		try {
			log.info("{}->开始进行登录中。。。。{} ",port , user);
			return userService.login(user);
		} catch (Exception e) {
			log.info("服务提供方=》用户密码登录服务异常 : {} " , e );
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	@PostMapping(value = "/wechatlogin")
	public ResponseEntity<?> wechatlogin(@RequestBody User user) {
		try {
			log.info("{}->开始进行登录中。。。。{} ",port , user);
			return userService.wechatLogin(user);
		} catch (Exception e) {
			log.info("服务提供方=》用户微信登录服务异常 : {} " , e );
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	@GetMapping(value = "/config")
	public String getconfig(){
		try {
			TimeUnit.SECONDS.sleep(sleep);
			return port;
		}catch (InterruptedException e){}
		return info;
	}
	
	
	@GetMapping(value = "/toptypelist")
	public ResponseEntity<?> toptypelist(){
		Map<String,Object> retMap;
		List<Map<String,Object>> ret = new ArrayList<Map<String,Object>>();
		retMap = new HashMap<String, Object>();
		retMap.put("typeId", 12);
		retMap.put("typeName", "水果");
		ret.add(retMap);
		retMap = new HashMap<String, Object>();
		retMap.put("typeId", 13);
		retMap.put("typeName", "蔬菜");
		ret.add(retMap);
		retMap = new HashMap<String, Object>();
		retMap.put("typeId", 14);
		retMap.put("typeName", "日化");
		ret.add(retMap);
		retMap = new HashMap<String, Object>();
		retMap.put("typeId", 15);
		retMap.put("typeName", "特价");
		ret.add(retMap);
		return new ResponseEntity<>(new ResponseResultModel(ret), HttpStatus.OK);
	}

}
